package com.lwt.spock.service;

import cn.hutool.core.util.StrUtil;
import com.lwt.spock.dao.MoneyDao;
import com.lwt.spock.dao.UserDao;
import com.lwt.spock.exception.BusinessException;
import com.lwt.spock.pojo.dto.UserDTO;
import com.lwt.spock.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lwt
 * @version 1.0
 * @Description
 * @date 2021/8/14 20:51
 */
@Service
public class UserService {
    @Resource
    UserDao userDao;
    public UserVO getUserById(Integer uid){
        List<UserDTO> users =  userDao.getUserInfoList();
        UserDTO userDTO = users.stream().filter(u -> u.getId() == uid).findFirst().orElse(null);
        UserVO userVO = new UserVO();
        if(null == userDTO){
            return userVO;
        }
        userVO.setId(userDTO.getId());
        userVO.setName(userDTO.getName());
        userVO.setSex(userDTO.getSex());
        userVO.setAge(userDTO.getAge());
        // 显示邮编
        if("上海".equals(userDTO.getProvince())){
            userVO.setAbbreviation("沪");
            userVO.setPostCode(200000);
        }
        if("北京".equals(userDTO.getProvince())){
            userVO.setAbbreviation("京");
            userVO.setPostCode(100000);
        }
        // 手机号处理
        if(null != userDTO.getTelephone() && !"".equals(userDTO.getTelephone())){
            userVO.setTelephone(userDTO.getTelephone().substring(0,3)+"****"+userDTO.getTelephone().substring(7));
        }

        return userVO;
    }

    public void validateUser(UserDTO userDTO) throws BusinessException {
        if (userDTO == null) {
            throw new BusinessException("10001", "用户为null");
        }
        if (StrUtil.isBlank(userDTO.getName())) {
            throw new BusinessException("10002", "name is null");
        }
    }

}
